#!/bin/env bash
# version : 0.1
# auth : fanyongfeng
# date : 2023-10-11
# script aim : install mongodb single node 4.0.28
# 
# tgz 安装 https://www.mongodb.com/docs/v4.4/tutorial/install-mongodb-on-red-hat-tarball/
# https://www.cnblogs.com/hxlasky/p/14023084.html

service_name=mongodb
decompression_dir=/data
pkg_install_dir=/data/${service_name}
pkg_install_version=4.0.28
pkg_name=${service_name}-linux-x86_64-rhel70-${pkg_install_version}
port=27017
MONGODB_USER="admin"
MONGODB_PASS=$(tr -dc '_A-Z#\-+=a-z(0-9%^>)]{<|' </dev/urandom | head -c 15; echo)

portNum=`ss -ntpl | grep ${port} | wc -l`

function del_var(){
    unset service_name
    unset decompression_dir
    unset pkg_install_dir
    unset pkg_install_version
    unset pkg_name
    unset port
    unset MONGODB_USER
    unset MONGODB_PASS
    unset portNum
}


if [[ $portNum -gt 0 ]];then
    echo "tcp port ${port} already use try to other port !!!"
    exit 1
fi

if [[  ! -f  "${pkg_name}.tgz" ]] ; then
    echo "${pkg_name}.tgz package  is not exist"
    exit 1
fi


## 安装 nginx 软件安装的依赖
printf "\n<<<<<<<< Installing dependent Software\n\n"

## 测试每一个安装包，没有下载的进行安装
pack_name=(
    libcurl
    openssl
    xz-libs
)

grep 'ky' /etc/os-release >/dev/null 2>&1
if [ $? -eq 0 ];then
    if [[  ! -f  "compat-openssl10-1.0.2o-3.el8.x86_64.rpm" ]] ; then
        echo "compat-openssl10-1.0.2o-3.el8.x86_64.rpm package  is not exist"
        exit 1
    fi
    yum install -y compat-openssl10-1.0.2o-3.el8.x86_64.rpm
fi

for tmp_pack_name in ${pack_name[*]}; do
    rpm -q $tmp_pack_name &>/dev/null || yum install -y $tmp_pack_name
done

tar -xf ${pkg_name}.tgz -C ${decompression_dir}
mv ${decompression_dir}/${pkg_name} ${decompression_dir}/${service_name}
ln -s  ${pkg_install_dir}/bin/* /usr/local/bin/

mkdir -p ${pkg_install_dir}/{data,log,run,conf}
if ! id -u ${service_name} >/dev/null 2>&1;then 
    useradd -M ${service_name}  -M -s /bin/false
fi

cat << EOF > ${pkg_install_dir}/conf/mongod.conf 
port=${port}
fork=true
dbpath=${pkg_install_dir}/data
logpath=${pkg_install_dir}/log/mongodb.log
pidfilepath=${pkg_install_dir}/run/${port}.pid
logappend=true
bind_ip=`hostname -I | awk '{print $1}'`,127.0.0.1
oplogSize=10000
logRotate=reopen
auth=true
EOF

chown -R ${service_name}:${service_name} ${pkg_install_dir}

cat << EOF >  /etc/systemd/system/${service_name}.service
[Unit]
Description=Mongodb Service
After=network.target remote-fs.target nss-lookup.target
[Service]
User=${service_name}
Group=${service_name}
Type=forking
ExecStart=${pkg_install_dir}/bin/mongod --config ${pkg_install_dir}/conf/mongod.conf 
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=${pkg_install_dir}/bin/mongod --config ${pkg_install_dir}/conf/mongod.conf --shutdown
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start ${service_name}
sleep 15
systemctl status ${service_name} --no-pager 

printf "\n<<<<<<<< Installing dependent Software\n\n"
${pkg_install_dir}/bin/mongo  $(hostname -I | awk '{print $1}'):${port} << EOF
use admin
db.createUser({
  user: "${MONGODB_USER}",
  pwd: "${MONGODB_PASS}",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
EOF

echo -e "mongodb admin user account: ${MONGODB_USER} ${MONGODB_PASS}"

del_var